package com.deng.struts.web.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;

public class FileCopyTest {
	public static void main(String[] args) throws IOException {

		FileCopyTest ioTest = new FileCopyTest();
		File uploadFile = new File("E:/apache-maven-3.0.5-bin.zip");
		int fileLength = Integer.parseInt(uploadFile.length()+"");
		byte[] messageFiles = ioTest.getFileMessage(uploadFile,fileLength);
		
		int subLen = 1024*1000;
		byte[] subMessageFiles = new byte[subLen];
		byte[] subMessageFiles1 = new byte[messageFiles.length-subLen];
		
		int j = 0;
		for(int i=0; i<messageFiles.length; i++){
			if(i<subLen){
				subMessageFiles[i] = messageFiles[i];
			}else{
				subMessageFiles1[j++] = messageFiles[i];
			}
		}
		
		ioTest.upload(subMessageFiles, 0);
		ioTest.upload(subMessageFiles1, 1024*1000);
	}
	
	public byte[] getFileMessage(File file,int len) throws IOException{
		InputStream input = null;
		
		try {
			byte[] fileMessages = new byte[len];
			input = new FileInputStream(file);
			input.read(fileMessages);
			return fileMessages;
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			input.close();
		}
		
		return null;
	}

	public void upload(byte[] fileMessage,int off) throws IOException {
		RandomAccessFile savedFile = null;
				
		try {
			savedFile = new RandomAccessFile("E:/iotest/test.zip", "rw");
			savedFile.seek(off);
			savedFile.write(fileMessage);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			savedFile.close();
		}
	}
}
